package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.Result;
import com.youlai.system.mapper.StockProviderMapper;
import com.youlai.system.model.entity.StockMedicines;
import com.youlai.system.model.entity.StockProvider;
import com.youlai.system.service.StockProviderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @作业 chen
 * @时间 2024-09-05 9:53
 */
@RestController
@RequestMapping("/api/v1/StockProvider")
@RequiredArgsConstructor
@Slf4j
public class StockProviderController {
    private final StockProviderMapper stockProviderMapper;

    @RequestMapping("/list")
    public Result list(@RequestBody StockProvider stockProvider) {
        System.out.println("stockProvider:" + stockProvider);
        System.out.println("stockProvider:" + stockProvider.getCurrentPage4());
        System.out.println("stockProvider:" + stockProvider.getPageSize4());
        Page<StockProvider> page = new Page(stockProvider.getCurrentPage4(), stockProvider.getPageSize4());
        LambdaQueryWrapper<StockProvider> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!stockProvider.getProviderName().isEmpty(), StockProvider::getProviderName, stockProvider.getProviderName());
        wrapper.like(!stockProvider.getContactName().isEmpty(), StockProvider::getContactName, stockProvider.getContactName());
        wrapper.like(!stockProvider.getContactMobile().isEmpty(), StockProvider::getContactMobile, stockProvider.getContactMobile());
        wrapper.like(!stockProvider.getStatus().isEmpty(), StockProvider::getStatus, stockProvider.getStatus());
        Page<StockProvider> page1 = stockProviderMapper.selectPage(page, wrapper);
        return Result.success(page1);
    }


    @RequestMapping("/add")
    public Result add(@RequestBody StockProvider stockProvider) {
        LambdaQueryWrapper<StockProvider> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StockProvider::getProviderName, stockProvider.getProviderName());
        wrapper.eq(StockProvider::getContactName, stockProvider.getContactName());
        wrapper.eq(StockProvider::getContactMobile, stockProvider.getContactMobile());
        wrapper.eq(StockProvider::getContactTel, stockProvider.getContactTel());
        wrapper.eq(StockProvider::getBankAccount, stockProvider.getBankAccount());
        wrapper.eq(StockProvider::getProviderAddress, stockProvider.getProviderAddress());
        wrapper.eq(StockProvider::getStatus, stockProvider.getStatus());
        List<StockProvider> list = stockProviderMapper.selectList(wrapper);
        if (list.size() > 0) {
            return Result.failed("该供应商信息已存在！");
        }
        int result = stockProviderMapper.insert(stockProvider);
        if (result > 0) {
            return Result.success("新增供应商信息成功！");
        } else {
            return Result.failed("新增供应商信息失败！");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody StockProvider stockProvider) {
//        providerId: "",
//                providerName: "",
//                contactName: "",
//                contactMobile: "",
//                contactTel: "",
//                bankAccount: "",
//                providerAddress: "",
//                status: "",
        LambdaUpdateWrapper<StockProvider> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StockProvider::getProviderId, stockProvider.getProviderId());
        wrapper.set(StockProvider::getProviderName, stockProvider.getProviderName());
        wrapper.set(StockProvider::getContactName, stockProvider.getContactName());
        wrapper.set(StockProvider::getContactMobile, stockProvider.getContactMobile());
        wrapper.set(StockProvider::getContactTel, stockProvider.getContactTel());
        wrapper.set(StockProvider::getBankAccount, stockProvider.getBankAccount());
        wrapper.set(StockProvider::getProviderAddress, stockProvider.getProviderAddress());
        wrapper.set(StockProvider::getStatus, stockProvider.getStatus());
        int result = stockProviderMapper.update(null, wrapper);
        if (result > 0) {
            return Result.success("修改供应商信息成功！");
        } else {
            return Result.failed("修改供应商信息失败！");
        }

    }


    @RequestMapping("/delete/{providerId}")
    public Result delete(@PathVariable("providerId") Long id) {
        LambdaQueryWrapper<StockProvider> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(StockProvider::getProviderId, id);
        int result = stockProviderMapper.delete(wrapper);
        if (result > 0) {
            return Result.success("删除供应商信息成功！");
        } else {
            return Result.failed("删除供应商信息失败！");
        }
    }

}

